﻿using System;
using System.Collections.Generic;
using System.Text;
using PluginInterface;

namespace t2
{

    // Informacije o funkcijah se nahajajo v IEpgPlugin.cs (PluginInterface)
    public class EpgPlugin : IEpgPlugin
    {
        // podatki o vtičniku
        private string name = "t2";
        private string comment = "";
        private string author = "MulticastTV";
        private string version = "0.1";
        private string multicasttvVersion = "0.31.0.2";
        private string lockedToLang = "sl-si";
        private string type = "online";

        //nastavitve
        private string hostname = "http://tv.t-2.net/";
        private string httpMethod = "GET";
        private string initRequest = "";
        private string initHttpMethod = "GET";
        private bool initRequestNeeded = false;  // Ne potrebujemo - uporabno le če rabimo
                                                 //kakšno variablo za določen dan.
        IPluginHost myHost = null;

        public IPluginHost Host
        {
            get { return myHost; }
            set { myHost = value; }
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        public EpgPlugin()
        {

        }

        public string Author
        {
            get { return author; }
        }

        public string Version
        {
            get { return version; }
        }

        public string MulticasttvVersion
        {
            get { return multicasttvVersion; }
        }

        public string Comment
        {
            get { return comment; }
        }

        public string Name
        {
            get { return name; }
        }

        public string PluginType
        {
            get { return type; }
        }

        public string LockedToUserInterfaceLangue
        {
            get { return lockedToLang; }
        }

        public string HostName
        {
            get { return hostname; }
        }

        public string RequestMethod
        {
            get { return httpMethod; }
        }

        public string InitialRequest
        {
            get { return initRequest; }
        }

        public bool InitialRequestNeeded
        {
            get { return initRequestNeeded; }
            set { initRequestNeeded = value; }
        }

        public string InitialRequestMethod
        {
            get { return initHttpMethod; }
        }


        public void Initialize(string Path)
        {

        }

        public void Dispose()
        {

        }

        public string[] GetTodayRequest(string ChannelId)
        {
            string request = "";
            string referer = "";
            string postString = "";

            request = "?TVCHUID=" + ChannelId;
            referer = hostname ;


           string[] RequestParams = {request,    // http request
                                     referer ,    // http referer
                                     postString  // POST string -  to je potrebno le za POST metodo
                                     };
            return RequestParams;
        }

        public string[] GetTodaySunmmaryRequest()
        {
            // To še ni implementirano.
            
            string[] RequestParams = { };
            return RequestParams;
        }

        public void ParseInitPage(string Html)
        {
            // ne potrebujemo
        }

        public List<string[]> ParseToday(string Html)
        {
            // Bolj robustno bi bilo seveda uporabiti Regex.
           
            List<string[]> Plist = new List<string[]>();
            Plist.Clear();
           
            // najprej ekstrahirajmo tabelo
            int table_start = Html.IndexOf("CMSSuperTitle");
            if (table_start == -1)
                return Plist;
            
            // tabela verjetno obstaja - poskusimo najti njen konec.
            int table_stop = Html.IndexOf("</table>",table_start);
            string tabela="";

            if (table_stop != -1)
            {
                tabela = Html.Substring(table_start, table_stop - table_start);
                int row_start = tabela.IndexOf("<tt>");
                
                while (row_start != -1)
                {
                    string vrstica="";
                    int row_stop = tabela.IndexOf("</tr>", row_start);
                    if (row_stop != -1 && row_stop > row_start)
                    {
                        // ekstrahirajmo vrstico
                        vrstica = tabela.Substring(row_start, row_stop - row_start);
                        string[] data = extractRow(vrstica);
                        if (data[0].Length > 0)
                            Plist.Add(data);
                    }
                    else
                        row_stop = row_start + 1;
                    
                    row_start = tabela.IndexOf("<tt>",row_stop);
                }
            }
       
            return Plist;
        }

        public string[] ParseDetails(string Html)
        {
            // To ne bo nikoli klicano, ker na strani http://tv.t-2.net/ ni detajlov o oddaji.
            
            string title = "";
            string genre = "";
            string description = "";
            string channelName = "";
            string time = "";
            string actors = "";
            string actorsTitle = "";
            string director = "";
            string directorTitle = "";
            string detailsLink = "";

            return new string[] { title, genre, description, channelName, time, actors, actorsTitle, director, directorTitle, detailsLink };
        }


        public List<string[]> ParseTodaySummary(string Html)
        {
            // To še ni implementirano.

            List<string[]> Plist = new List<string[]>();
            Plist.Clear();
            return Plist;
        }

        //helper functions

        private string[] extractRow(string vrstica)
        {
            string[] data = new string[4];
            string ura = ""; // je že v pravem formatu
            string naslov = "";
            string tip = "";
            string link = "0";
            
            int ura_stop = vrstica.IndexOf("</tt>");
            if (ura_stop != -1)
                ura = vrstica.Substring(4, ura_stop-4)+" - ";

            int naslov_start = vrstica.IndexOf("<b>");
            if (naslov_start != -1)
            {
                naslov_start=naslov_start+3;
                int naslov_stop = vrstica.IndexOf("</b>", naslov_start);
                if (naslov_stop != -1)
                    naslov = vrstica.Substring(naslov_start,naslov_stop-naslov_start);
            }

            int tip_start = vrstica.IndexOf(@"<div class=""small-text"">");
            if (tip_start != -1)
            {   
                tip_start = tip_start+24;
                int tip_stop = vrstica.IndexOf("</div>");
                if (tip_stop != -1)
                {
                    tip = vrstica.Substring(tip_start, tip_stop - tip_start);
                }
            }

            data[0] = ura;    // time  "hh:mm - "  
            data[1] = naslov; // title
            data[2] = tip;    // genre
            data[3] = link;   // details link  "0" če ni detajlov

            return data;
        }
    }

}
